//
// Created by test on 23-3-5.
//
#include<time.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<stdio.h>
#include<pthread.h>
#include<sys/epoll.h>
#include<fcntl.h>
#include<errno.h>
#define BUF_SIZE 64
class util_timer;
struct client_data{
    sockaddr_in address;
    int sockfd;
    char buf[BUF_SIZE];
    util_timer* timer;
};
class util_timer{
public:
    util_timer():prev(NULL),next(NULL){}
    time_t expire;
    util_timer* prev;
    util_timer* next;
    client_data* user_data;
    void (*cb_func)(client_data*);
};
class sort_timer_lst{
private:
    util_timer* head;
    util_timer* tail;
public:
    sort_timer_lst():head(NULL),tail(NULL){};
    ~sort_timer_lst(){
        util_timer* tmp=head;
        while(tmp){
            head=head->next;
            delete tmp;
            tmp=head;
        }
    }
    void add_timer(util_timer* timer){
        if(timer==NULL){
            return;
        }
        if(head==NULL){
            head=tail=timer;
            return;
        }
        if(timer->expire<head->expire){
            timer->next=head;
            head->prev=timer;
            head=timer;
            head->prev=NULL;
            return;
        }
        add_timer(timer,head);
    }
    void add_timer(util_timer* timer,util_timer* head){
        util_timer* prev=head;
        util_timer* temp=prev->next;
        while(temp!=NULL&&temp->expire<=timer->expire){
            if(timer->expire<temp->expire){
                temp->prev=timer;
                timer->next=temp;
                timer->prev=prev;
                prev->next=timer;
                break;
            }
            prev=prev->next;
            temp=temp->next;
        }
        if(temp==NULL){
            prev->next=timer;
            timer->prev=prev;
            timer->next=NULL;
            tail=timer;
        }
    }
    void adjust_timer(util_timer* timer){
        if(timer==NULL){
            return;
        }
        util_timer* temp=head->next;
        util_timer* pre=head;
        if(timer==head){
            temp=head;
            head=head->next;
        }
        else{
            while(temp!=timer){
                temp=temp->next;
                pre=pre->next;
            }
            pre->next=temp->next;
            if(temp->next!=NULL){
                temp->next->prev=pre;
            }
        }
        temp->next=NULL;
        temp->prev=NULL;
        add_timer(temp,head);
    }
    void del_timer(util_timer* timer){
        if(timer==NULL){
            return;
        }
        if(head==NULL){
            return;
        }
        if(timer==head&&head==tail){
            head=NULL;
            tail=NULL;
            delete timer;
            return;
        }
        util_timer* temp=head->next;
        if(timer==head){
            temp=head;
            head->next->prev=NULL;
            temp->next=NULL;
            delete temp;
        }
        util_timer* pre=head;
        while(temp!=timer){
            temp=temp->next;
            pre=pre->next;
        }
        pre->next=temp->next;
        if(temp!=tail){
            temp->next->prev=pre;
        }
        temp->prev=NULL;
        temp->next=NULL;
        delete temp;
    }
    void tick(){
        if(head==NULL){
            return;
        }
        printf("tick\n");
        time_t cur_time=time(NULL);
        util_timer* temp=head;
        while(temp!=NULL){
            if(cur_time<temp->expire){
                break;
            }
            temp->cb_func(temp->user_data);
            head=temp->next;
            if(head!=NULL){
                head->prev=NULL;
            }
            delete temp;
            temp=head;
        }
    }
};